﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace CH.Easy.OpenLogin
{
    public class QZoneOauthV2 : AbstractOauthV2
    {
        public QZoneOauthV2()
        {
            this.InitConfig();
        }
        public QZoneOauthV2(string callBack)
        {
            this.InitConfig();
            base.CallBack = callBack;
        }

        public QZoneOauthV2(string accessToken, string openID)
        {
            this.InitConfig();
            base.AccessToken = accessToken;
            base.OpenID = openID;
        }
        public QZoneOauthV2(string accessToken, string openID,string callBack)
        {
            this.InitConfig();
            base.AccessToken = accessToken;
            base.OpenID = openID;
            base.CallBack = callBack.Replace("http://", "");
        }
        private void InitConfig()
        {
            base.OpenType = OpenType.QZone;
            base.AuthorizeUrl = "https://graph.qq.com/oauth2.0/authorize";
            base.TokenUrl = "https://graph.qq.com/oauth2.0/token";
            base.MeUrl = "https://graph.z.qq.com/moc2/me";
            base.CallBack = Common.GetConfig("CallBackUrl").Replace("http://", "") + "/common/open/callback.aspx";
            base.AppKey = Common.GetConfig("QZoneAppkey");
            base.AppSecret = Common.GetConfig("QZoneAppSecret");
            base.OpenRequest = new OpenRequest();
        }

        public override string GetAuthorizeUrl()
        {
            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"response_type","code"},
                {"client_id",base.AppKey},
                {"redirect_uri",base.CallBack},
                {"scope","get_user_info,add_t,add_pic_t,get_info"},
                {"state",Guid.NewGuid().ToString().Replace("-","").ToLower()}

            };

            return base.AuthorizeUrl + "?" + param.GetQueryString();
        }
        public override AccessTokenDTO GetAccessToken(string code)
        {
            AccessTokenDTO accessTokenDTO = null;
            if (string.IsNullOrEmpty(code))
                return accessTokenDTO;


            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"grant_type","authorization_code"},
                {"client_id",base.AppKey},
                {"client_secret",base.AppSecret},
                {"redirect_uri",base.CallBack},
                {"code",code},
                {"state",Guid.NewGuid().ToString().Replace("-","").ToLower()}

            };
            
            try
            {
                string result = base.OpenRequest.HttpGet(base.TokenUrl, param.GetQueryString());
                if (result.Contains("=") && result.Contains("&"))
                {
                    Dictionary<string, string> list = result.GetParam();
                    accessTokenDTO = new AccessTokenDTO();
                    accessTokenDTO.access_token = list["access_token"];
                    accessTokenDTO.expires_in = list["expires_in"];
                }
                else
                {
                    //如果返回来的数据是json 格式
                    result = result.Replace("callback(", "").Replace(");", "");
                    accessTokenDTO = JsonConvert.DeserializeObject<AccessTokenDTO>(result);
                }
            }
            catch
            {

                return null;
            }

            return accessTokenDTO;
        }

     

        public override CallBackDTO GetOpenID(string accessToken)
        {
            CallBackDTO callBackDTO = null;
            if (string.IsNullOrEmpty(accessToken))
                return callBackDTO;


            Dictionary<string, string> param = new Dictionary<string, string>()
            {
                {"access_token",accessToken}
            };
            
            
            try
            {
                string result = base.OpenRequest.HttpGet(base.MeUrl, param.GetQueryString());
                if (result.Contains("=") && result.Contains("&")) 
                {
                    Dictionary<string, string> list = result.GetParam();
                    callBackDTO = new CallBackDTO();
                    callBackDTO.openid = list["openid"];
                    callBackDTO.client_id = list["client_id"];
                }
                else
                {
                    //如果返回来的数据是json 格式
                    result = result.Replace("callback(", "").Replace(");", "");
                    callBackDTO = JsonConvert.DeserializeObject<CallBackDTO>(result);
                }
            }
            catch
            {

                return null;
            }

            return callBackDTO;
        }
    }
}
